package com.example.demo.common;

import cn.hutool.core.math.MathUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;

import java.awt.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.channels.Pipe;
import java.text.DecimalFormat;
import java.util.*;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class Test06 {

    public static void main(String[] args) {

        List<JSONObject> pList = JSON.parseArray("[{\"canonicalImage\":\"https://b2b-material.cdn.bcebos.com/xBetnPt4zpN27cvaXZwPzBACP34.jpg\",\"name\":\"慕源 框架护栏网 铁路公路防护网 绿色浸塑围栏 果园隔离栅\",\"price\":[{\"minValue\":1,\"maxValue\":100,\"price\":35,\"unitCode\":\"平方米\"},{\"minValue\":100,\"maxValue\":1000,\"price\":30,\"unitCode\":\"平方米\"},{\"minValue\":1000,\"price\":20,\"unitCode\":\"平方米\"}],\"status\":\"60\"},{\"canonicalImage\":\"https://b2b-material.cdn.bcebos.com/1GD_FOHWHx003NkHE9OqOhAEd2c.jpg\",\"name\":\"慕源 蓝白色市政>护栏网 道路交通隔离网 人车分流防护网\",\"price\":[{\"minValue\":1,\"maxValue\":100,\"price\":30,\"unitCode\":\"米\"},{\"minValue\":100,\"maxValue\":1000,\"price\":20,\"unitCode\":\"米\"},{\"minValue\":1000,\"price\":8,\"unitCode\":\"米\"}],\"status\":\"60\"},{\"canonicalImage\":\"https://b2b-material.cdn.bcebos.com/5_UgW5r7IuCBvLrSJ6yn7hABmLM.jpg\",\"name\":\"国外防攀爬网围栏 铁丝358防爬护栏网 y型柱密文网 慕源\",\"price\":[\"52\",\"60\"],\"status\":\"60\"},{\"canonicalImage\":\"https://b2b-material.cdn.bcebos.com/Hu4biQRcv_kAP0wYxUuSlRAFUu0.jpg\",\"name\":\"慕源 包塑勾花球场围网 体育场护栏网 操场隔离网 抗晒防腐\",\"price\":[\"35\",\"40\"],\"status\":\"60\"},{\"canonicalImage\":\"http://t11.baidu.com/it/u=1035592821,2223777880&fm=199&app=68&f=JPEG?w=750&h=750&s=C03118D556A6C7EF0834A44E03003070\",\"name\":\"慕源 gps柔性山坡落石边坡防护网 主动网 用途防护坡面危岩\",\"price\":[\"30\",\"50\"],\"status\":\"60\"},{\"canonicalImage\":\"https://b2b-material.cdn.bcebos.com/AG8PJAl2rIuIhpWePzH-fBAB514.jpg\",\"name\":\"慕源生产 防��隔离网 MY-74保税区围网 安装便捷 车间护栏网\",\"price\":[\"30\",\"50\"],\"status\":\"60\"},{\"canonicalImage\":\"http://t11.baidu.com/it/u=3017128996,1619288308&fm=199&app=68&f=JPEG?w=750&h=750&s=EE2305C7CA32D3EF0628353803001010\",\"name\":\"慕源 边坡主动网 主动防护挂网不易腐蚀 山体防滑网 河道支护网\",\"price\":[{\"minValue\":\"1.00\",\"priceCurrency\":\"元\",\"maxValue\":\"\",\"price\":\"7.6\",\"unitCode\":\"平方米\"}],\"status\":\"60\"},{\"canonicalImage\":\"https://b2b-material.cdn.bcebos.com/AG8PJAl2rIuIhpWePzH-fBAB514.jpg\",\"name\":\"仓库护栏网 车间隔离网 厂房规划用网 浸塑焊接网 慕源\",\"price\":[\"20\",\"25\"],\"status\":\"60\"},{\"canonicalImage\":\"http://t10.baidu.com/it/u=1452212430,1633168848&fm=199&app=68&f=JPEG?w=750&h=750&s=90B179915A7E5A8E2601C8F0010010B3\",\"name\":\"慕源 道路声屏障 高铁公路隔音墙 构成隔音板立柱附带配件\",\"price\":[{\"minValue\":\"1.00\",\"priceCurrency\":\"元\",\"maxValue\":\"\",\"price\":\"134\",\"unitCode\":\"米\"}],\"status\":\"60\"},{\"canonicalImage\":\"http://t11.baidu.com/it/u=300454503,3432099741&fm=199&app=68&f=JPEG?w=750&h=750&s=953E5790D226EAEC5E302C540300C0B2\",\"name\":\"慕源 铁丝防攀爬护>栏网 Y型柱刺丝隔离网 焊接钢筋网编制金属网\",\"price\":[{\"minValue\":\"1.00\",\"priceCurrency\":\"元\",\"maxValue\":\"\",\"price\":\"35\",\"unitCode\":\"平方米\"}],\"status\":\"60\"}]", JSONObject.class);

        HashMap<String, List<String>> map = new HashMap<>();
        map.put("NAME", pList.stream()
                .map(x -> x.getString("name")).collect(Collectors.toList()));
        map.put("IMG", pList.stream()
                .map(x -> x.getString("canonicalImage")).collect(Collectors.toList()));
        //map.put("PRICE", pList.stream()
        //       .map(x -> x.getJSONArray("price").getJSONObject(0).getString("price")).collect(Collectors.toList()));

        map.put("PRICE", pList.stream()
                .map(x -> {
                    JSONArray priceArray = x.getJSONArray("price");
                    if (priceArray.toJSONString().contains("price")) {
                        return priceArray.getJSONObject(0).getString("price");
                    }
                    return priceArray.getString(0);
                }).collect(Collectors.toList()));


        System.out.println(map.get("123"));

        System.out.println(JSONObject.toJSONString(map));


        JSONArray jsonArray1 = JSONArray.parseArray("[\n" +
                "                {\n" +
                "                    \"minValue\": 1,\n" +
                "                    \"price\": 10000,\n" +
                "                    \"unitCode\": \"套\"\n" +
                "                }\n" +
                "            ]");
        System.out.println(jsonArray1);
        JSONArray jsonArray = JSONArray.parseArray("[]");
        if (!jsonArray.isEmpty()) {
            JSONObject firstItem = jsonArray.getJSONObject(0);
            BigDecimal price = firstItem.getBigDecimal("price");
            if (price.compareTo(new BigDecimal(10000)) < 0) {
                firstItem.put("price", price.setScale(2, RoundingMode.HALF_UP) + "元");
            } else {
                firstItem.put("price", price.divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP) + "万");
            }
        } else {
            jsonArray.set(0, JSON.parseObject("{\"price\":\"面议\"}"));
        }
        System.out.println(jsonArray);


        BigDecimal price = new BigDecimal(11128765);
        if (price.compareTo(new BigDecimal(10000)) < 0) {
            System.out.println(price.setScale(2, RoundingMode.HALF_UP) + "元");
        } else {
            System.out.println(price.divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP) + "万");
        }


        List<Integer> demoList = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

        System.out.println(JSONObject.toJSONString(demoList.stream().filter(x -> x % 2 == 0).collect(Collectors.toList())));
        System.out.println(JSONObject.toJSONString(demoList.stream().filter(x -> x % 3 == 0).collect(Collectors.toList())));
        System.out.println(JSONObject.toJSONString(demoList.stream().filter(x -> x % 4 == 0).collect(Collectors.toList())));

        System.out.println(JSONObject.toJSONString(demoList));

    }

    public static <T> T loopGetList(List<T> list, int index) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        int size = list.size();
        index = index - size * (index / size);

        return list.get(index);
    }
}
